package com.prs.crm.action.crm.report;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.crm.Rival;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.crm.RivalService;

public class RivalReportAction extends ReportAction {

	private static final long serialVersionUID = 6883380870793376519L;
	private Rival rival;
	private ChartResult chartResult;
	private ChartService chartService;
	private RivalService rivalService;
	private PaginationSupport pagination;

	private String category;
	private int exportType;

	@Action("editRivalLevel")
	public String editRivalLevel() {
		return SUCCESS;
	}

	@Action("editRivalNature")
	public String editRivalNature() {
		return SUCCESS;

	}

	@Action(value = "viewRivalLevelPie", results = { @Result(name = "success", type = "chain", location = "editRivalLevel") })
	public String viewRivalLevelPie() {
		Collection<?> dataList = this.getRivalService().getReportLevelSource();
		chartResult = this.getChartService().export(
				this.getRivalService().fillDataset(dataList,
						Constrains.REPORT_PIECHART), "对手威胁级别分析",
				"listReportRivalLevel");
		return SUCCESS;
	}

	@Action(value = "viewRivalLevelBar", results = { @Result(name = "success", type = "chain", location = "editRivalLevel") })
	public String viewRivalLevelBar() {
		Collection<?> dataList = this.getRivalService().getReportLevelSource();
		chartResult = this.getChartService().export(
				this.getRivalService().fillDataset(dataList,
						Constrains.REPORT_BARCHART), "对手威胁级别分析", "数量", "",
				"listReportRivalLevel");
		return SUCCESS;
	}

	@Action(value = "viewRivalNaturePie", results = { @Result(name = "success", type = "chain", location = "editRivalNature") })
	public String viewRivalNaturePie() {
		Collection<?> dataList = this.getRivalService().getReportNatureSource();
		chartResult = this.getChartService().export(
				this.getRivalService().fillDataset(dataList,
						Constrains.REPORT_PIECHART), "对手企业性质分析",
				"listReportRivalNature");
		return SUCCESS;
	}

	@Action(value = "viewRivalNatureBar", results = { @Result(name = "success", type = "chain", location = "editRivalNature") })
	public String viewRivalNatureBar() {
		Collection<?> dataList = this.getRivalService().getReportNatureSource();
		chartResult = this.getChartService().export(
				this.getRivalService().fillDataset(dataList,
						Constrains.REPORT_BARCHART), "对手企业性质分析", "数量", "",
				"listReportRivalNature");
		return SUCCESS;
	}

	@Action("listReportRivalLevel")
	public String listReportRivalLevel() {
		category = ReportUtils.URLDecode(this.getCategory());
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rival.class);
		criteria.createAlias("rivalLevel", "rk");
		criteria.add(Restrictions.eq("rk.level", category));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRivalService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listReportRivalNature")
	public String listReportRivalNature() {
		category = ReportUtils.URLDecode(this.getCategory());
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rival.class);
		criteria.createAlias("rivalNature", "rk");
		criteria.add(Restrictions.eq("rk.nature", category));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRivalService().listByPage(criteria,
				pagination));

		return SUCCESS;
	}

	@Action(value = "exportRivalLevelReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportRivalLevelReport() {
		ReportTable table = new ReportTable("对手威胁级别报表");
		table.addCell(new ReportCell("序号", "id"));
		table.addCell(new ReportCell("对手名称", "name"));
		table.addCell(new ReportCell("编号", "code"));
		table.addCell(new ReportCell("威胁级别", "rivalLevel.level"));
		table.addCell(new ReportCell("企业性质", "rivalNature.nature"));
		table.addCell(new ReportCell("地点", "district.name"));
		table.addCell(new ReportCell("联系电话", "officePhone"));
		table.addCell(new ReportCell("电子邮箱", "email"));
		table.addCell(new ReportCell("网址", "url"));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rival.class);
		criteria.createAlias("rivalLevel", "rk");
		criteria.add(Restrictions.eq("rk.level", this.getCategory()));
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	@Action(value = "exportRivalNatureReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportRivalNatureReport() {
		ReportTable table = new ReportTable("对手企业性质分析");
		table.addCell(new ReportCell("序号", "id"));
		table.addCell(new ReportCell("对手名称", "name"));
		table.addCell(new ReportCell("编号", "code"));
		table.addCell(new ReportCell("威胁级别", "rivalLevel.level"));
		table.addCell(new ReportCell("企业性质", "rivalNature.nature"));
		table.addCell(new ReportCell("地点", "district.name"));
		table.addCell(new ReportCell("联系电话", "officePhone"));
		table.addCell(new ReportCell("电子邮箱", "email"));
		table.addCell(new ReportCell("网址", "url"));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Rival.class);
		criteria.createAlias("rivalNature", "rk");
		criteria.add(Restrictions.eq("rk.nature", this.getCategory()));
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	public Rival getRival() {
		return rival;
	}

	public void setRival(Rival rival) {
		this.rival = rival;
	}

	public ChartResult getChartResult() {
		return chartResult;
	}

	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}

	public ChartService getChartService() {
		return chartService;
	}

	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}

	public RivalService getRivalService() {
		return rivalService;
	}

	public void setRivalService(RivalService rivalService) {
		this.rivalService = rivalService;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public int getExportType() {
		return exportType;
	}

	public void setExportType(int exportType) {
		this.exportType = exportType;
	}

}
